package com.shu.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.shu.dao.StudentDao;
import com.shu.entity.Student;

@Repository("studentDao")
public class StudentDaoImpl  implements StudentDao{
	
	@Autowired
	private SessionFactory sessionFactory;

	public Session getSession() {
		
		return sessionFactory.getCurrentSession();
		
	}
	@Override
	public void insertStudent(Student student) {
		this.getSession().save(student);
	}

	@Override
	public float countMaxScore(String course) {
		String sql = "select * from student_inf "
				+ "where stu_score=(select max(stu_score) from student_inf where stu_course=?) "
				+ "and stu_course=?";
//		this.getSession().createQuery("");
//		this.getSession().
//		this.getHibernateTemplate().find("from Student");
		SQLQuery query = this.getSession().createSQLQuery(sql);
		query.setString(0, course);
		query.setString(1, course);
		query.addEntity(Student.class);
		List<Student> lists =query.list();
//		Student student=(Student) query.uniqueResult();
		Student student=lists.get(0);
		return student.getScore();
	}

	@Override
	public float countMinScore(String course) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public float countAvgScore(String course) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<Student> countMaxScoreList(String course) {
		String sql = "select * from student_inf "
				+ "where stu_score=(select max(stu_score) from student_inf where stu_course=?) "
				+ "and stu_course=?";
//		this.getSession().createQuery("");
//		this.getSession().
//		this.getHibernateTemplate().find("from Student");
		SQLQuery query = this.getSession().createSQLQuery(sql);
		query.setString(0, course);
		query.setString(1, course);
		query.addEntity(Student.class);
		List<Student> lists =query.list();
//		Student student=(Student) query.uniqueResult();
		return lists;
	}

}
